<!--员工管理-->
<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header style="height: 70px;margin: 10px;">
      <div class="search-container">
        <el-form style="padding: 50px;">
          <el-form-item label="姓名：" style="margin-bottom: 0;width: calc(25% - 20px);">
            <el-input placeholder="请输入姓名" clearable v-model="empForm.username"></el-input>
          </el-form-item>
          <el-form-item label="手机号：" style="margin-bottom: 0;width: calc(25% - 20px);">
            <el-input placeholder="请输入手机号" clearable v-model="empForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="所属学校：" style="margin-bottom: 0;width: calc(25% - 20px);">
            <el-select placeholder="请选择" clearable v-model="empForm.school">
              <el-option v-for="item in schoolArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户状态：" style="margin-bottom: 0;width: calc(25% - 20px);">
            <el-select placeholder="请选择" clearable v-model="empForm.status">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <div class="button-group" style="position: relative">
            <div class="center-buttons">  <!-- 新增居中按钮容器 -->
              <el-button type="primary" style="padding: 10px;" @click="selectEmp">查询</el-button>
              <el-button style="padding: 10px;" @click="resetEmp">重置</el-button>
            </div>
            <el-button type="danger" icon="el-icon-plus" style="position: absolute;right: 20px;padding: 10px;"
                       @click="beforeAddUser">新增员工
            </el-button>
          </div>
        </el-form>
      </div>
    </el-header>
    <!-- 主内容区域 -->
    <el-main style="margin-top: 20px">
      <div class="table-container">
        <div class="table-header">
          <div class="table-title">
            <i class="el-icon-s-grid"></i>
            <span>员工列表</span>
          </div>
          <div>
            <el-tag type="info">共{{ total }}位员工</el-tag>
          </div>
        </div>

        <el-table :data="employees" style="width: 100%;margin: 0 auto">
          <el-table-column prop="id" label="ID" sortable></el-table-column>
          <el-table-column prop="username" label="姓名"></el-table-column>
          <el-table-column prop="phone" label="手机号"></el-table-column>
          <el-table-column prop="gender" label="性别" :formatter="genderTypeFormatter"></el-table-column>
          <el-table-column prop="school" label="所属学校" :formatter="schoolTypeFormatter"></el-table-column>
          <el-table-column prop="role" label="角色" :formatter="roleTypeFormatter"></el-table-column>
          <el-table-column prop="status" label="状态" :formatter="statusTypeFormatter"></el-table-column>
          <el-table-column label="操作" width="200" style="position: relative">
            <template #default="scope">
              <div class="operation-cell" style="position: absolute;right: 110px;top: 8px;">
                <el-button size="small" type="primary" style="padding: 10px;" @click="beforeEdit(scope.row.id)">编辑
                </el-button>
                <el-button
                    size="small"
                    :type="scope.row.status === '0' ? 'danger' : 'success'"
                    style="padding: 10px;"
                    @click="changeStatus(scope.row.id,scope.row.status)"
                >
                  {{ scope.row.status === '0' ? '禁用' : '启用' }}
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination">
          <el-pagination
              :current-page="1"
              :page-size="10"
              :total="8"
              layout="prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </div>
    </el-main>
    <!-- 弹框区域  -->
    <el-dialog
        :title="dialogTitle"
        style="width: 800px;padding: 40px;"
        v-model="dialogVisible"
        :before-close="handleClose"
    >
      <el-form label-position="top">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="用户名" style="width: 350px;">
              <el-input placeholder="请输入用户名" v-model="saveUserForm.username"
                        :disabled="saveUserForm.id!=null"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" style="width: 350px;">
              <el-input placeholder="请输入手机号" v-model="saveUserForm.phone"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12" style="width: 350px;">
            <el-form-item label="学校">
              <el-select placeholder="请选择" v-model="saveUserForm.school" @change="loadLeader">
                <el-option v-for="item in schoolArr" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="width: 350px;">
            <el-form-item label="角色">
              <el-select placeholder="请选择" v-model="saveUserForm.role">
                <el-option v-for="item in roleArr" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="性别" style="width: 100px;">
              <el-radio-group v-model="saveUserForm.gender">
                <el-radio value="1" style="margin: 0;" label="男"></el-radio>
                <el-radio value="0" label="女"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户状态" style="width: 200px;">
              <el-radio-group v-model="saveUserForm.status">
                <el-radio border value="1" style="margin: 0;" label="启用"></el-radio>
                <el-radio border value="0" label="禁用"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <el-button @click="handleClose" style="width: 80px;">取消</el-button>
        <el-button type="primary" @click="saveUser" style="width: 80px;">保存</el-button>
      </template>
    </el-dialog>
  </el-container>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
//
var total;
//
const dialogTitle = ref('新增员工');
//
const dialogVisible = ref(false);
//
const empForm = ref(
    {
      username: '',
      phone: '',
      school: '',
      status: ''
    }
);
//
const schoolArr = ref([
  {
    label: '清华大学',
    value: '10'
  },
  {
    label: '北京大学',
    value: '20'
  },
  {
    label: '南京大学',
    value: '30'
  },
  {
    label: '浙江大学',
    value: '40'
  }
  // ['清华大学','北京大学','浙江大学','南京大学']
]);
const roleArr = ref([
  {
    label: '实习',
    value: '10'
  },
  {
    label: '教师',
    value: '20'
  },
  {
    label: '教导主任',
    value: '30'
  },
  {
    label: '校长',
    value: '40'
  }
  // ['清华大学','北京大学','浙江大学','南京大学']
]);
//
const employees = ref([]);
//
const resetEmp = () => {
  empForm.value = {};
}
//
const selectEmp = () => {
  let data = qs.stringify(empForm.value);
  axios.get(BASE_URL + '/v1/employee/select?' + data)
      .then((response) => {
        if (response.data.code == 2000) {
          employees.value = response.data.data;
          total = employees.value.length;
        } else {
          ElMessage.error(response.data.message);
        }
      })
}
onMounted(() => {
  selectEmp();
})
//
//处理员工类型列显示文字转换 10=>清华大学
const schoolTypeFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '清华大学';
  } else if (cellValue == 20) {
    cellValue = '北京大学';
  } else if (cellValue == 30) {
    cellValue = '南京大学';
  } else if (cellValue == 40) {
    cellValue = '浙江大学';
  }
  return cellValue;
}
//处理员工类型列显示文字转换 10=>实习
const roleTypeFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '实习';
  } else if (cellValue == 20) {
    cellValue = '教师';
  } else if (cellValue == 30) {
    cellValue = '教导主任';
  } else if (cellValue == 40) {
    cellValue = '校长';
  }
  return cellValue;
}
//处理员工类型列显示文字转换 0=>女
const genderTypeFormatter = (row, column, cellValue, index) => {
  if (cellValue == 0) {
    cellValue = '女';
  } else if (cellValue == 1) {
    cellValue = '男';
  }
  return cellValue;
}
//处理员工类型列显示文字转换 0=>启用
const statusTypeFormatter = (row, column, cellValue, index) => {
  if (cellValue == 0) {
    cellValue = '启用';
  } else if (cellValue == 1) {
    cellValue = '禁用';
  }
  return cellValue;
}
//
const changeStatus = (id, status) => {
  axios.post(BASE_URL + '/v1/employee/update/' + id + '/' + status)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('更改成功');
          selectEmp();
        } else {
          ElMessage.error(response.data.message);
        }
      })
}
//
const handleClose = () => {
  if (confirm('您确定要关闭弹窗吗')) {
    saveUserForm.value = {};//清空用户填写的未提交数据
    dialogVisible.value = false;//关闭弹窗
  }
}
//
const saveUser = () => {
  let data = qs.stringify(saveUserForm.value);
  console.log(data);
  axios.post(BASE_URL + '/v1/employee/save', data)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('保存成功');
          dialogVisible.value = false;
          saveUserForm.value = {};
          selectEmp();
        } else if (response.data.code == 3001) {
          ElMessage.error(response.data.data);
        } else {
          ElMessage.error(response.data.message);
        }
      })
}
//
const beforeAddUser = () => {
  dialogTitle.value = '新建员工';
  dialogVisible.value = true;
}
//
const beforeEdit = (id) => {
  dialogTitle.value = '编辑员工';
  dialogVisible.value = true;
  axios.get(BASE_URL + '/v1/employee/select?id=' + id)
      .then((response) => {
        if (response.data.code == 2000) {
          saveUserForm.value = response.data.data[0];
        } else {
          ElMessage.error(response.data.message);
        }
      })
}
//
const saveUserForm = ref(
    {
      username: '',
      phone: '',
      school: '',
      role: '',
      gender: '',
      status: ''
    }
)

</script>


<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', Arial, sans-serif;
}

body {
  background-color: #f5f7fa;
  color: #333;
  height: 100vh;
  overflow: hidden;
}

.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-header {
  background: linear-gradient(135deg, #dfe5ef 0%, #e4e9ef 100%);
  color: white;
  padding: 0 20px;
  height: auto !important;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.search-container {
  width: 100%;
  max-width: 1600px;
  padding: 20px 0;
}

.el-form {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

/*.el-form-item {*/
/*  margin-bottom: 0;*/
/*  width: calc(25% - 20px);*/
/*}*/

.button-group {
  display: flex;
  justify-content: space-between; /* 左右分列 */
  align-items: center;
  width: 100%;
  margin-top: 15px;
  gap: 15px;
  position: relative; /* 为居中定位提供参考 */
}

/* 新增居中按钮容器样式 */
.center-buttons {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 15px;
}

.el-main {
  background-color: #f0f2f5;
  padding: 0 !important;
  height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.table-container {
  flex: 1;
  padding: 25px;
  display: flex;
  flex-direction: column;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.table-title {
  font-size: 18px;
  color: #2c3e50;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  flex: 1;
}

.status-active {
  color: #67C23A;
  font-weight: 500;
}

.status-inactive {
  color: #F56C6C;
  font-weight: 500;
}

.pagination {
  margin-top: 25px;
  display: flex;
  justify-content: flex-end;
}

.operation-cell {
  display: flex;
  gap: 8px;
}

.male-tag {
  background-color: #ecf5ff;
  color: #409EFF;
  border: 1px solid #d9ecff;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.female-tag {
  background-color: #fdf6ec;
  color: #e6a23c;
  border: 1px solid #f5dab1;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.el-button--warning {
  background-color: #e6a23c;
  border-color: #e6a23c;
}

@media (max-width: 1200px) {
  .el-form-item {
    width: calc(50% - 20px);
  }
}

@media (max-width: 768px) {
  .el-form-item {
    width: 100%;
  }

  .button-group {
    flex-direction: column;
  }

  .center-buttons {
    position: static;
    transform: none;
    order: 1; /* 在移动端将按钮放在顶部 */
    margin-bottom: 10px;
  }

  .button-group > .el-button:last-child {
    order: 2; /* 在移动端将新增按钮放在底部 */
    width: 100%;
  }
}
</style>